package jp.co.cos.ittrick.channel.controller.client;

import java.util.List;

import jp.co.cos.ittrick.channel.meta.ChapterModelMeta;
import jp.co.cos.ittrick.channel.model.CategoryModel;
import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.service.CategoryService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class GetChapterListController extends Controller {

    private CategoryService categoryService;

    public GetChapterListController() {
        categoryService = new CategoryService();
    }

    @Override
    protected Navigation run() throws Exception {

        String category = asString("category");

        CategoryModel categoryModel = categoryService.getCategoryByKey(Integer.valueOf(category));
        List<ChapterModel> chapterList = categoryModel.getChapterModelListRef().getModelList();

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        Object[] entityArray = chapterList.toArray();
        response.getWriter().println(ChapterModelMeta.get().modelsToJson(entityArray));
        response.flushBuffer();

        return null;
    }

}
